package com.newedo.projects.lfx.dao.impl;

import com.newedo.projects.lfx.dao.itf.IAddressDAO;
import com.newedo.projects.lfx.entity.Address;
import com.newedo.projects.lfx.entity.MapPoint;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * 地址数据操作接口实现类
 */
public class AddressDAOImpl implements IAddressDAO {
    private EntityManagerFactory factory;
    private EntityManager em;
    public AddressDAOImpl(String pu_name){
        this.factory = Persistence.createEntityManagerFactory(pu_name);
        this.em = factory.createEntityManager();
    }
    @Override
    public List<Address> getAll() throws Exception{
        try{
            List resultList = em.createQuery("from Address").getResultList();//获得结果列表
            return resultList;
        } catch (Exception e) {
            throw new Exception(e.getMessage(),e);
        } finally{
            em.close();
            factory.close();
        }
    }

    @Override
    public Address findById(Long id) throws Exception {
        try{
            return em.find(Address.class, id);//Hibernate:get()非延迟，不缓存，null
        } catch (Exception e) {
            em.getTransaction().rollback();
            throw new Exception(e.getMessage(),e);
        } finally{
            em.close();
            factory.close();
        }
    }

    @Override
    public void saveAll(List<Address> vos) throws Exception{
        try {
            if(vos != null && !vos.isEmpty()){
                em.getTransaction().begin();
                for (Address address : vos) {
                    if(address.getId() == null || (address.getId() != null && em.find(Address.class, address.getId()) == null)){
                        //新增
                        em.persist(address);
                    }else{
                        //更新
                        em.merge(address);
                    }
                }
                em.getTransaction().commit();
            }
        } catch (Exception e) {
            em.getTransaction().rollback();
            throw new Exception(e.getMessage(),e);
        } finally {
            em.close();
            factory.close();
        }
    }

    @Override
    public void deleteAll(List<Address> vos) throws Exception{
        try {
            if(vos != null && !vos.isEmpty()){
                em.getTransaction().begin();
                for (Address address : vos) {
                    Address findResult = em.find(Address.class, address.getId());
                    if(findResult != null){
                        //删除
                        em.remove(findResult);
                    }
                }
                em.getTransaction().commit();
            }
        } catch (Exception e) {
            em.getTransaction().rollback();
            throw new Exception(e.getMessage(),e);
        } finally {
            em.close();
            factory.close();
        }
    }

    @Override
    public void deleteAll(Long[] ids) throws Exception{
        try {
            if(ids != null && ids.length > 0){
                em.getTransaction().begin();
                for (Long id : ids) {
                    Address findResult = em.find(Address.class, id);
                    if(findResult != null){
                        //删除
                        em.remove(findResult);
                    }
                }
                em.getTransaction().commit();
            }
        } catch (Exception e) {
            em.getTransaction().rollback();
            throw new Exception(e.getMessage(),e);
        } finally {
            em.close();
            factory.close();
        }
    }

    @Override
    public void deleteAll() throws Exception{
        deleteAll(getAll());
    }
}
